package com.zzw.hj25;

import java.util.*;

/**
 * @Project: hw_java
 * @Description: 数据分类处理
 * @Author: zzw
 */
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Integer, List<int[]>> sequence2 = new TreeMap<>();
        int[] sequence1 = null;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            sequence1 = new int[a];
            for (int i = 0; i < a; i++) {
                sequence1[i] = in.nextInt();
            }
            int b = in.nextInt();
            for (int i = 0; i < b; i++) {
                sequence2.put(in.nextInt(), new ArrayList<>());
            }
        }
        if (sequence1 != null) {
            for (int i = 0; i < sequence1.length; i++) {
                final String sequence = sequence1[i] + "";
                final int i1 = i;
                sequence2.forEach((key, value) -> {
                    if (sequence.contains(key + "")) {
                        value.add(new int[]{i1, Integer.parseInt(sequence)});
                    }
                });
            }
        }
        ;
        List<Integer> sq = new ArrayList<>();
        sequence2.forEach((key, value) -> {
            if (value.size() > 0) {
                sq.add(key);
                sq.add(value.size());
                value.forEach(s -> {
                    sq.add(s[0]);
                    sq.add(s[1]);
                });
            }
        });
        System.out.printf(sq.size() + " ");
        sq.forEach(ss -> System.out.print(ss + " "));

    }
}